package com.xuecheng.media.api;


import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.model.dto.PageParams;
import com.xuecheng.base.model.dto.PageResult;
import com.xuecheng.base.model.dto.RestResponse;
import com.xuecheng.media.model.dto.QueryMediaParamsDto;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.model.dto.UploadFileResultDto;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author Mr.M
 * @version 1.0
 * @description 媒资文件管理接口
 * @date 2022/9/6 11:29
 */
@Api(value = "媒资文件管理接口", tags = "媒资文件管理接口")
@RestController
public class MediaFilesController {


    @Autowired
    MediaFileService mediaFileService;


    @ApiOperation("媒资列表查询接口")
    @PostMapping("/files")
    public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto) {
        Long companyId = 1232141425L;
        return mediaFileService.queryMediaFiles(companyId, pageParams, queryMediaParamsDto);

    }

    /**
     * 上传图片接口
     *
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @ApiOperation("上传图片接口")
    @PostMapping(value = "/upload/coursefile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadFileResultDto upload(@RequestPart("filedata") MultipartFile multipartFile,
                                      @RequestParam( value = "objectName",required = false) String objectName) throws IOException {
        Long companyId = 1232141425L;
        //文件大小、名称都从MultipartFile中取
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        uploadFileParamsDto.setFileType("001001");
        uploadFileParamsDto.setFileSize(multipartFile.getSize());
        uploadFileParamsDto.setFilename(multipartFile.getOriginalFilename());
        //创建临时文件，用于获取本地绝对路径
        File tempFile = File.createTempFile("minio", ".temp");
        multipartFile.transferTo(tempFile);
        //取本地绝对路径
        String absolutePath = tempFile.getAbsolutePath();
        UploadFileResultDto upload = mediaFileService.upload(companyId, absolutePath, uploadFileParamsDto,objectName);
        return upload;

    }

    /**
     * 根据MD5检查分块文件接口
     *
     * @param fileMd5
     * @return
     */
    @ApiOperation("检查分块文件接口")
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> checkChunk(@RequestParam String fileMd5) {
        RestResponse<Boolean> booleanRestResponse = mediaFileService.checkChunk(fileMd5);
        return booleanRestResponse;
    }

    /**
     * 上传分块
     *
     * @param file       前端提交分块文件
     * @param fileMd5    分块文件MD5
     * @param chunkIndex 第几个分块
     * @return
     */
    @ApiOperation("检查分块文件接口")
    @PostMapping("/upload/uploadchunk")
    public RestResponse<Boolean> uploadChunk(@RequestPart("file") MultipartFile file, @RequestParam String fileMd5, @RequestParam("chunk") int chunkIndex) {
        if (file == null || StringUtils.isEmpty(fileMd5)) {
            XueChengPlusException.cast("上传的文件或者其他参数为空");
        }

        File tempFile = null;
        try {
            tempFile = File.createTempFile("minio", ".temp");
            file.transferTo(tempFile);
        } catch (IOException e) {
            e.printStackTrace();
            XueChengPlusException.cast("创建临时文件出错");
        }
        String absolutePath = tempFile.getAbsolutePath();

        RestResponse<Boolean> booleanRestResponse = mediaFileService.uploadChunk(absolutePath, fileMd5, chunkIndex);
        return booleanRestResponse;
    }

    /**
     * 合并分块
     *
     * @param fileMd5
     * @param fileName
     * @param chunkTotal
     * @return
     */
    @ApiOperation("合并分块")
    @PostMapping("/upload/mergechunks")
    public RestResponse mergeChunks(@RequestParam("fileMd5") String fileMd5,
                                    @RequestParam("fileName") String fileName,
                                    @RequestParam("chunkTotal") int chunkTotal) {
        Long companyId = 1232141425L;
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        uploadFileParamsDto.setFilename(fileName);
        RestResponse restResponse = mediaFileService.mergeChunks(companyId, uploadFileParamsDto, chunkTotal, fileMd5);
        return restResponse;
    }

}
